class Admins::CluesController < ApplicationController
  before_action :set_clue, only: [:show, :edit, :update, :destroy,:submit,:approve,:reject]
  before_action :new_clue, only: [:show, :new, :edit, :update, :destroy,:submit,:approve,:reject]
  # GET /notifies
  # GET /notifies.json
  def index
    clue_type = params[:clue_type] || '1'
    @departments = Department.all
    session[:department] = params[:department] if params[:department]
    
    @clues = Clue.clue_type(clue_type,current_user).check(params[:check]).contribution_units(params[:department]).page(params[:page])
  end

  # GET /notifies/1
  # GET /notifies/1.json
  def show
  end

  # GET /notifies/new
  def new
    @departments = Department.all
    @clue = Clue.new
  end

  # GET /notifies/1/edit
  def edit
    @departments = Department.all
  end

  # POST /notifies
  # POST /notifies.json
  def create
    @clue = Clue.new(clue_params.merge({postby: current_user.name}))
    respond_to do |format|
      if @clue.save
        format.html { redirect_to admins_clues_url, notice: '新闻线索创建成功.' }
      else
        flash[:error] = @clue.errors.full_messages.join(",")
        format.html { render :new }
      end
      
      unless params[:groups].blank?
         groups = params[:groups]
         admin_group = Group.where(:name => '管理员').first
         groups << admin_group.id unless groups.include?(admin_group.id.to_s)
         groups.each do |group_id|
           ClueGroup.create(:clue_id => @clue.id,:group_id => group_id.to_i)
         end
       end
    end
  end

  def submit
    respond_to do |format|
      if @clue.update_attributes(check: 1)
        format.html { redirect_to admins_clues_path @clue, notice: '新闻线索送审成功.' }
      else
        format.html { render :edit }
      end
    end
  end

  #采用
  def approve
    @clue.update_attributes(check: 2)
    redirect_to admins_clues_path @clue, notice: '新闻线索采用成功.'
  end

  #不采用
  def reject
    @clue.update_attributes(check: 3,reason: params[:reason])
    redirect_to admins_clues_path @clue, notice: '新闻线索退回成功.'
  end

  # PATCH/PUT /notifies/1
  # PATCH/PUT /notifies/1.json
  def update
    respond_to do |format|
      if @clue.update(clue_params)
          unless params[:groups].blank?
             ClueGroup.where(:clue_id => @clue.id).delete_all
             groups = params[:groups]
             admin_group = Group.where(:name => '管理员').first
             groups << admin_group.id unless groups.include?(admin_group.id.to_s)
             groups.each do |group_id|
             ClueGroup.create(:clue_id => @clue.id,:group_id => group_id.to_i)
         end
       end
        format.html { redirect_to admins_clues_url, notice: '新闻线索修改成功' }
      else
        format.html { render :edit }
      end
      
     
    end
  end

  # DELETE /notifies/1
  # DELETE /notifies/1.json
  def destroy
    @clue.destroy
    respond_to do |format|
      format.html { redirect_to admins_clues_url, notice: '新闻线索删除成功' }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
     # Use callbacks to share common setup or constraints between actions.
    def set_clue
      @clue = Clue.find(params[:id])
    end

   def new_clue
      @groups = Group.all.where(:name => ['管理员','校报组','外宣组'])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def clue_params
      params.require(:clue).permit(:linkman, :phone, :content,:contribution_units)
    end
end
